package com.keur.money.ui.splash.impl

import android.content.Context
import com.keur.money.http.IHttpCallback
import com.keur.money.mvp.presenter.MvpPresenter
import com.keur.money.ui.splash.view.IKeurSplashView
import com.keur.money.utils.helper.KeurCustomConfigsHelper
import org.json.JSONObject

/**
 * MullaFlow
 * @Author wwdeng
 * @CreateTime: 2024/7/25 13:46
 * @Annotation：启动页 Presenter 实现
 */
class KeurSplashPresenterImpl(context: Context?) : MvpPresenter<IKeurSplashView>(context) {

    private var model: KeurSplashModelImpl? = null
    init {
        model = KeurSplashModelImpl()
    }

    /**
     * 获取地区转发
     */
    fun loadMullaTimeZoneAction(){
        model?.loadMullaTimeZoneAction(object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                when(data.optInt("code")) {
                    0 -> {//成功
                        val dataString = data.optString("data","")
                        mvpView?.showMullaTimeZoneRequestData(dataString)
                    }
                    else -> {
                        val msg = data.optString("msg")
                        mvpView?.showMullaToast(msg)
                        mvpView?.hideMullaLoading()
                    }
                }
            }
            override fun onRequestError(error: JSONObject) {
                mvpView?.showMullaToast(error.optString("errorMsg"))
                mvpView?.hideMullaLoading()
            }
            override fun onRequestFinish() {
            }
        })
    }

    /**
     * 获取App统一配置信息
     */
    fun loadMullaCommonConfigAction(){
        model?.loadMullaCommonConfigAction(object : IHttpCallback{
            override fun onRequestSuccess(data: JSONObject) {
                when(data.optInt("code")) {
                    0 -> {//成功
                        mvpView?.hideMullaLoading()
                        val obj = data.optJSONObject("obj")
                        //保存所有数据
                        if (obj != null) {
                            KeurCustomConfigsHelper.onSaveKeurConfigData(obj.toString())
                        }
                        mvpView?.showKeurLoadConfigSuccess()
                    }

                    else -> {
                        val msg = data.optString("msg")
                        mvpView?.showMullaToast(msg)
                    }
                }
            }

            override fun onRequestError(error: JSONObject) {
                mvpView?.showMullaToast(error.optString("errorMsg"))
            }

            override fun onRequestFinish() {
                mvpView?.hideMullaLoading()
            }
        })
    }
}